<?php

class SearchController extends Controller {

//    public $activeMenuItem = 'support';
    public $cssFile = '/css/search.css';

	public function actionIndex() {
        $this->pageTitle = Yii::t('app', 'Поиск фирм');
        //$cs = Yii::app()->clientScript;
        //$cs->registerScriptFile(Yii::app()->request->baseUrl . '/js/search.js', CClientScript::POS_END);
        /*
        $searchForm = new SearchFirmsForm;
        $searchkey = get_class($searchForm);
        
        if (isset($_POST[$searchkey])) {
            $searchForm->attributes = $_POST[$searchkey];
        } else {
            if (isset(Yii::app()->session[$searchkey])) {
                $searchForm->attributes = Yii::app()->session[$searchkey];
            } 
        } 
		*/		
        //Debug::dump( $searchForm->attributes);
        
        $criteria = new CDbCriteria();
		$criteria->compare('t.hidden', 0);
    $criteria->compare('t.deleted', 0);
        $criteria->compare('t.active', 1);
        $searchq_key = isset($_REQUEST['sq']) ? $_REQUEST['sq'] : '';
		if('' != $searchq_key ) {
			$criteria->compare('t.title', $searchq_key, true);
			$criteria->compare('t.description', $searchq_key, true, 'OR');
		}		
		
        $criteria->compare('t.country_id', $this->getYiiP('default_country_id'));
		
        if ($this->checkCookie('region_id')) $criteria->compare('t.region_id', intval($this->getCookie('region_id')));
		if ($this->checkCookie('city_id')) $criteria->compare('t.city_id', intval($this->getCookie('city_id')));
        
        $count = Firms::model()->count($criteria);
        $criteria->order = 't.rating_order DESC, t.title ASC';
        
        $pages = new CPagination($count);
        $pages->pageSize = Yii::app()->params['list_firms_perpage'];
        $pages->applyLimit($criteria);
        
        //$data = Firms::model()->with('firmTmpl', 'rubric', 'category', 'country', 'region', 'city')->findAll($criteria);
        $data = Firms::model()->findAll($criteria);
//        Debug::dump($data);
        $this->render('index', array(
			'count' => $count,
            'firms' => $data,
            'pages' => $pages,
			'searchq_key' => $searchq_key,
        ));
	}

}